<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - 论坛系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .register-container {
            background: white;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            text-align: center;
        }

        .register-header {
            margin-bottom: 30px;
        }

        .register-header h2 {
            color: #333;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .register-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            background: white;
        }

        .form-group input.error {
            border-color: #e74c3c;
            background: #fdf2f2;
        }

        .form-group input.success {
            border-color: #27ae60;
            background: #f2fdf5;
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #27ae60;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .password-requirements {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-top: 10px;
            font-size: 13px;
            text-align: left;
        }

        .password-requirements h4 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 14px;
        }

        .requirement {
            display: flex;
            align-items: center;
            margin-bottom: 5px;
            color: #7f8c8d;
        }

        .requirement.valid {
            color: #27ae60;
        }

        .requirement-icon {
            margin-right: 8px;
            font-size: 12px;
        }

        .register-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }

        .register-btn:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .register-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .message.error {
            background-color: #fee;
            color: #c33;
            border: 1px solid #fcc;
        }

        .message.success {
            background-color: #efe;
            color: #363;
            border: 1px solid #cfc;
        }

        .login-link {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .login-link:hover {
            color: #764ba2;
            text-decoration: underline;
        }

        .divider {
            margin: 20px 0;
            text-align: center;
            position: relative;
        }

        .divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: #e1e5e9;
        }

        .divider span {
            background: white;
            padding: 0 15px;
            color: #666;
            font-size: 14px;
        }

        @media (max-width: 480px) {
            .register-container {
                padding: 30px 20px;
                margin: 10px;
            }

            .register-header h2 {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
<div class="register-container">
    <div class="register-header">
        <h2>创建账户</h2>
        <p>加入我们的论坛社区</p>
    </div>

    <% String msg = (String) request.getAttribute("msg"); %>
    <% if (msg != null && !msg.isEmpty()) { %>
    <div class="message <%= msg.contains("成功") ? "success" : "error" %>"><%= msg %></div>
    <% } %>

    <form id="registerForm" action="/register" method="post">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required placeholder="请输入用户名">
            <div class="error-message" id="usernameError"></div>
            <div class="success-message" id="usernameSuccess"></div>
        </div>

        <div class="form-group">
            <label for="email">邮箱地址</label>
            <input type="email" id="email" name="email" required placeholder="请输入邮箱地址">
            <div class="error-message" id="emailError"></div>
            <div class="success-message" id="emailSuccess"></div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required placeholder="请输入密码">
            <div class="error-message" id="passwordError"></div>
            <div class="success-message" id="passwordSuccess"></div>

            <div class="password-requirements">
                <h4>密码要求：</h4>
                <div class="requirement" id="lengthReq">
                    <span class="requirement-icon">○</span>
                    <span>至少8个字符</span>
                </div>
                <div class="requirement" id="letterReq">
                    <span class="requirement-icon">○</span>
                    <span>包含英文字母</span>
                </div>
                <div class="requirement" id="numberReq">
                    <span class="requirement-icon">○</span>
                    <span>包含数字</span>
                </div>
                <div class="requirement" id="specialReq">
                    <span class="requirement-icon">○</span>
                    <span>包含特殊字符 (!@#$%^&*)</span>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="confirmPassword">确认密码</label>
            <input type="password" id="confirmPassword" name="confirmPassword" required placeholder="请再次输入密码">
            <div class="error-message" id="confirmPasswordError"></div>
            <div class="success-message" id="confirmPasswordSuccess"></div>
        </div>

        <button type="submit" class="register-btn" id="registerBtn" disabled>注册</button>
    </form>

    <div class="divider">
        <span>已有账户？</span>
    </div>

    <a href="login" class="login-link">立即登录</a>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const form = document.getElementById('registerForm');
        const username = document.getElementById('username');
        const email = document.getElementById('email');
        const password = document.getElementById('password');
        const confirmPassword = document.getElementById('confirmPassword');
        const registerBtn = document.getElementById('registerBtn');

        // 密码要求元素
        const lengthReq = document.getElementById('lengthReq');
        const letterReq = document.getElementById('letterReq');
        const numberReq = document.getElementById('numberReq');
        const specialReq = document.getElementById('specialReq');

        // 错误和成功消息元素
        const usernameError = document.getElementById('usernameError');
        const usernameSuccess = document.getElementById('usernameSuccess');
        const emailError = document.getElementById('emailError');
        const emailSuccess = document.getElementById('emailSuccess');
        const passwordError = document.getElementById('passwordError');
        const passwordSuccess = document.getElementById('passwordSuccess');
        const confirmPasswordError = document.getElementById('confirmPasswordError');
        const confirmPasswordSuccess = document.getElementById('confirmPasswordSuccess');

        // 密码验证函数
        function validatePassword(password) {
            const requirements = {
                length: password.length >= 8,
                letter: /[a-zA-Z]/.test(password),
                number: /\d/.test(password),
                special: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)
            };

            return requirements;
        }

        // 更新要求显示
        function updateRequirements(requirements) {
            updateRequirement(lengthReq, requirements.length);
            updateRequirement(letterReq, requirements.letter);
            updateRequirement(numberReq, requirements.number);
            updateRequirement(specialReq, requirements.special);
        }

        function updateRequirement(element, isValid) {
            const icon = element.querySelector('.requirement-icon');
            if (isValid) {
                element.classList.add('valid');
                icon.textContent = '✓';
            } else {
                element.classList.remove('valid');
                icon.textContent = '○';
            }
        }

        // 检查表单有效性
        function checkFormValidity() {
            const usernameValid = isUsernameValid();
            const emailValid = isEmailValid();
            const passwordValid = isPasswordValid();
            const confirmPasswordValid = isConfirmPasswordValid();

            registerBtn.disabled = !(usernameValid && emailValid && passwordValid && confirmPasswordValid);
        }

        function isUsernameValid() {
            return username.value.trim().length >= 3;
        }

        function isEmailValid() {
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            return emailRegex.test(email.value);
        }

        function isPasswordValid() {
            const requirements = validatePassword(password.value);
            return requirements.length && requirements.letter && requirements.number && requirements.special;
        }

        function isConfirmPasswordValid() {
            return confirmPassword.value.length > 0 && password.value === confirmPassword.value;
        }

        // 用户名验证
        username.addEventListener('input', function() {
            if (this.value.trim().length === 0) {
                hideError(this, usernameError);
                hideSuccess(usernameSuccess);
            } else if (this.value.trim().length < 3) {
                showError(this, usernameError, '用户名至少需要3个字符');
                hideSuccess(usernameSuccess);
            } else if (this.value.trim().length > 20) {
                showError(this, usernameError, '用户名不能超过20个字符');
                hideSuccess(usernameSuccess);
            } else {
                hideError(this, usernameError);
                showSuccess(this, usernameSuccess, '用户名格式正确');
            }
            checkFormValidity();
        });

        // 邮箱验证
        email.addEventListener('input', function() {
            if (this.value.length === 0) {
                hideError(this, emailError);
                hideSuccess(emailSuccess);
            } else if (isEmailValid()) {
                hideError(this, emailError);
                showSuccess(this, emailSuccess, '邮箱格式正确');
            } else {
                showError(this, emailError, '请输入有效的邮箱地址');
                hideSuccess(emailSuccess);
            }
            checkFormValidity();
        });

        // 密码验证
        password.addEventListener('input', function() {
            const requirements = validatePassword(this.value);
            updateRequirements(requirements);

            if (this.value.length === 0) {
                hideError(this, passwordError);
                hideSuccess(passwordSuccess);
            } else if (isPasswordValid()) {
                hideError(this, passwordError);
                showSuccess(this, passwordSuccess, '密码强度良好');
            } else {
                showError(this, passwordError, '密码不符合要求');
                hideSuccess(passwordSuccess);
            }

            // 如果确认密码已填写，重新验证
            if (confirmPassword.value.length > 0) {
                confirmPassword.dispatchEvent(new Event('input'));
            }

            checkFormValidity();
        });

        // 确认密码验证
        confirmPassword.addEventListener('input', function() {
            if (this.value.length === 0) {
                hideError(this, confirmPasswordError);
                hideSuccess(confirmPasswordSuccess);
            } else if (this.value === password.value) {
                hideError(this, confirmPasswordError);
                showSuccess(this, confirmPasswordSuccess, '密码匹配');
            } else {
                showError(this, confirmPasswordError, '两次输入的密码不一致');
                hideSuccess(confirmPasswordSuccess);
            }
            checkFormValidity();
        });

        // 显示错误
        function showError(input, errorElement, message) {
            input.classList.add('error');
            input.classList.remove('success');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }

        // 隐藏错误
        function hideError(input, errorElement) {
            input.classList.remove('error');
            errorElement.style.display = 'none';
        }

        // 显示成功
        function showSuccess(input, successElement, message) {
            input.classList.add('success');
            input.classList.remove('error');
            successElement.textContent = message;
            successElement.style.display = 'block';
        }

        // 隐藏成功
        function hideSuccess(successElement) {
            successElement.style.display = 'none';
        }

        // 表单提交验证
        form.addEventListener('submit', function(e) {
            if (!isUsernameValid() || !isEmailValid() || !isPasswordValid() || !isConfirmPasswordValid()) {
                e.preventDefault();
                alert('请确保所有字段都正确填写');
            }
        });
    });
</script>
</body>
</html>
